package com.cityfood.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.cityfood.common.pojo.CategoryFoods;
import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.SimpleProduct;
import com.cityfood.common.utils.JsonUtils;
import com.cityfood.pojo.CfAttributeKey;
import com.cityfood.pojo.CfCategory;
import com.cityfood.pojo.CfProduct;
import com.cityfood.service.AttributeKeyService;
import com.cityfood.service.CategoryService;
import com.cityfood.service.ProductService;
import com.github.pagehelper.PageInfo;

@Controller
public class CategoryController {
	
	@Autowired
	private CategoryService categoryService;
	 
	@Autowired
	private ProductService productService;
	
	@Autowired
	private AttributeKeyService keyService;
	
	/**
	 * 获取城市专区
	 */
	@RequestMapping(value="/categorys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCategoryANDProductByCityId(Integer cityId){
		CityFoodResult cityfoodResult = new CityFoodResult();
		//根据城市id查找所有的专区和专区里面的食品，包装类
		List<CategoryFoods> foodList = new ArrayList<CategoryFoods>();
		//根据城市id查找所有的专区
		List<CfCategory> categoryList = categoryService.getCategoryByCityId(cityId);
		if (categoryList.size()==0) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("当前城市无食品信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		for (CfCategory category : categoryList) {
			if (category.getStatus()!=0) {
				//专区展示包装类
				CategoryFoods food=new CategoryFoods();
				//首页商品展示包装类
				List<SimpleProduct> Products = new ArrayList<SimpleProduct>();
				//根据专区id查询到的所有商品
				List<CfProduct> productList = productService.getProductByCategoryId(category.getId());
				for (CfProduct product : productList) {
					if (product.getIsdisplay()!=0) {
						
						SimpleProduct pd = new SimpleProduct();
						//添加商品的id到商品展示类
						pd.setFoodId(product.getId());
						//添加商品的name到商品展示类
						pd.setFoodName(product.getName());
						//添加商品的标题到商品展示类
						pd.setFoodTitle(product.getSubTitle());
						//添加商品的图片路径到商品展示类
						pd.setPic(product.getMainImage());
						Products.add(pd);
					}
				}
				//包装类CategoryFoods添加属性
				//专区名称
				food.setType_name(category.getName());
				//专区名称标题
				food.setType_title(category.getTitle());
				//专区名称详细
				food.setType_detail(category.getNameDetail());
				//专区商品
				food.setProducts(Products);
				foodList.add(food);
			}
		}
		cityfoodResult.setCode(200);
		cityfoodResult.setMsg("获取食品列表成功");
		cityfoodResult.setData(foodList);
		return cityfoodResult;
	}
	
	//后台管理---------------------------------------------------------------------------
	
	/**
	 * 获取城市专区
	 */
	@RequestMapping(value="/manage/allCategorys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAllCategorys(){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		//根据城市id查找所有的专区
		List<CfCategory> categoryList = categoryService.getallCategorys();
		if (categoryList==null) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("获取食品类目失败");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>(); 
		for (CfCategory category : categoryList) {
//			包装要展示的数据
			Map<String,Object> ms = new LinkedHashMap<String,Object>();
			ms.put("id", category.getId());
			ms.put("name", category.getName());
			ms.put("title", category.getTitle());
			ms.put("keys", category.getKeyList());
			
			ls.add(ms);
		}
		cityfoodResult.setCode(200);
		cityfoodResult.setMsg("获取食品类目成功");
		cityfoodResult.setData(ls);
		return cityfoodResult;
	}
	
	
	/**
	 * 获取城市专区
	 */
	@RequestMapping(value="/manage/categorys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCategory(@RequestParam(defaultValue="0")Integer cityId,
									  @RequestParam(defaultValue="1")Integer currentPage){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		//根据城市id查找所有的专区
		PageInfo<CfCategory> categoryList = categoryService.getCategoryByCityIdAndPage(cityId, currentPage);
		if (categoryList==null) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("当前城市无食品信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>(); 
		for (CfCategory category : categoryList.getList()) {
//			包装要展示的数据
			Map<String,Object> ms = new LinkedHashMap<String,Object>();
			ms.put("id", category.getId());
			ms.put("parentId", category.getParentId());
			ms.put("name", category.getName());
			ms.put("title", category.getTitle());
			ms.put("detail", category.getNameDetail());
			ms.put("status", category.getStatus());
			ms.put("createTime", sd.format(category.getCreateTime()));
			ms.put("keys",category.getKeyList());
			ms.put("cityId", category.getCityId());
			ls.add(ms);
		}
		Map<String,Object> mp = new LinkedHashMap<>();
		//存入查询到的总数
		mp.put("total", categoryList.getTotal());
		//存入需要展示的数据
		mp.put("categorys", ls);
		
		cityfoodResult.setCode(200);
		cityfoodResult.setMsg("获取分类成功");
		cityfoodResult.setData(mp);
		return cityfoodResult;
	}
	
	
	@RequestMapping(value="/manage/categorys",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertCategory(@RequestBody Map<String,Object> map){
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfCategory category=new CfCategory();
		String parentId = map.get("parentId").toString();
		String cityId = map.get("cityId").toString();
		String name = map.get("name").toString();
		String title = map.get("title").toString();
		String nameDetail = map.get("nameDetail").toString();
		String keys = map.get("keys").toString();
		category.setCityId(Integer.parseInt(cityId)); //前端暂时没有录入的地方，默认深圳
		category.setParentId(Integer.parseInt(parentId));
		category.setName(name);
		category.setTitle(title);
		category.setNameDetail(nameDetail);
		category.setSortOrder(0); //分类关闭排序，按树结构展示
		category.setKeyList(keys);
		int i = categoryService.insertCategory(category);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("添加类目成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("添加类目失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	@RequestMapping(value="/manage/categorys",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateCategory(@RequestBody Map<String,Object> map){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		String id =  map.get("id").toString();
		String parentId = map.get("parentId").toString();
		String cityId = map.get("cityId").toString();
		String name = map.get("name").toString();
		String title = map.get("title").toString();
		String nameDetail = map.get("nameDetail").toString();
		String keys = map.get("keys").toString();
		CfCategory category=new CfCategory();
		category.setId(Integer.parseInt(id));
		category.setCityId(Integer.parseInt(cityId)); //前端暂时没有录入的地方，默认深圳
		category.setParentId(Integer.parseInt(parentId));
		category.setName(name);
		category.setTitle(title);
		category.setNameDetail(nameDetail);
		category.setSortOrder(0); //分类关闭排序，按树结构展示
		category.setKeyList(keys);
		int i = categoryService.updateCategory(category);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("修改类目成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("修改类目失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	@RequestMapping(value="/manage/categorys/remove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteCategory(@RequestBody Map map){
	    String id = map.get("id").toString();
	    String status = map.get("status").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfCategory category = new CfCategory();
		category.setId(Integer.parseInt(id));
		category.setStatus((byte)Integer.parseInt(status));
		int i = categoryService.deleteCategory(category);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("删除类目成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("删除类目失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	@RequestMapping(value="/manage/categorys/batchremove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteCategorys(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			CfCategory category = new CfCategory();
			category.setId(Integer.parseInt(strId));
			int i = categoryService.deleteCategory(category);
			if (i > 0) {
				cityfoodResult.setCode(200);
				cityfoodResult.setMsg("删除类目成功");
				cityfoodResult.setData(true);
			} else {
				cityfoodResult.setCode(201);
				cityfoodResult.setMsg("删除类目失败");
				cityfoodResult.setData(false);
				return cityfoodResult;
			}
		}
		return cityfoodResult;
	}

}
